package jafou.server;

import jafou.Model.Forum;
import jafou.Model.JaFouUser;
import jafou.Model.PMF;
import jafou.Model.Post;
import jafou.Model.Topic;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;

import com.google.appengine.api.datastore.Text;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class TopicsHandler extends RemoteServiceServlet {

	private JafouEnv env = new JafouEnv();
	
	
	public String[][] getMessages(String topicId) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String[][] result = new String[0][0];
		try {
			String query = "select from " + Post.class.getName()
					+ " where topicId == \"" + topicId + "\"";
			List<Post> postList = (List<Post>) pm.newQuery(query).execute();
			if (!postList.isEmpty()) {
				result = new String[postList.size()][3];
				for (int i = 0; i < result.length; i++) {
					Post post = postList.get(i);
					result[i][0] = post.getMessage().getValue();
					result[i][1] = env.getUserNameFromId(post.getUserId());
					result[i][2] = post.getCreatedOn().toString();
				}
			}
		} finally {
			pm.close();
		}
		return result;
	}

	
	public String postMessage(String topicId, String message) {
		if(env.getCurrentUser() == null) {
			return "";
		}
		String id = "";
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Post post = new Post(env.getCurrentUser().getId()+"", "testIP");
			post.setTopicId(topicId);
			post.setMessage(new Text(message!=null?message:""));
			pm.makePersistent(post);
			id = "" + post.getId();
		} finally {
			pm.close();
		}
		return id;
	}
	
	public Topic getTopic(String key) {
		Topic t = null;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			if(key != null){
				t = pm.getObjectById(Topic.class, Long.parseLong(key));
			}
		} finally {
			pm.close();
		}
		return t;
	}
	
	List<Topic> getTopics(Forum f) {
		List<Topic> topics = new ArrayList<Topic>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			if(f != null){
				String query = "select from " + Topic.class.getName()
					+ " where forumId == \"" + f.getId() + "\"";
				List<Topic> temp = (List<Topic>) pm.newQuery(query).execute();
				for (Topic t : temp)
				{
					topics.add(t);
				}
			}
		} finally {
			pm.close();
		}
		return topics;
	}
	
	public String addTopic(String forum, String message) {
		String id = "";
		JaFouUser user = env.getCurrentUser();
		if(user == null) {
			return "";
		}
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Topic topic = new Topic(""+user.getId(), "testIP", forum);
			topic.setSubject(message);
			pm.makePersistent(topic);
			id = "" + topic.getId();
		} finally {
			pm.close();
		}
		return id;
	}
	
	void delTopicsForForum(String forumId) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			String query = "select from " + Topic.class.getName()
					+ " where forumId == \"" + forumId + "\"";
			List<Topic> tList = (List<Topic>) pm.newQuery(query).execute();
			for(Topic t:tList) {
				delPostsForTopic(t.getId());
				pm.deletePersistent(t);
			}
		} finally {
			pm.close();
		}
	}
	
	void delPostsForTopic(String topicId) {
		try{
			long l = Long.parseLong(topicId.trim());
			delPostsForTopic(l);
		}catch (NumberFormatException nfe) {
		}
	}
	
	void delPostsForTopic(long topicId) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			String query = "select from " + Post.class.getName()
					+ " where topicId == \"" + topicId + "\"";
			List<Post> pList = (List<Post>) pm.newQuery(query).execute();
			for(Post p:pList) {
				pm.deletePersistent(p);
			}
		} finally {
			pm.close();
		}
	}


	public String delTopic(String topicId) {
		if(topicId == null || "".equals(topicId.trim())){ return ""; }
		delPostsForTopic(topicId);
		String forum = "";
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Topic t = pm.getObjectById(Topic.class, Long.parseLong(topicId));
			forum = t.getForumId();
			pm.deletePersistent(t);
		} finally {
			pm.close();
		}
		return forum;
	}


	public void delPost(String postId) {
		if(postId == null || "".equals(postId.trim())){ return; }
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Post t = pm.getObjectById(Post.class, Long.parseLong(postId));
			pm.deletePersistent(t);
		} finally {
			pm.close();
		}
	}

}
